package com.liu.sx.handler;

import com.liu.sx.common.util.ChannelHolder;
import com.liu.sx.entity.Worm;
import com.liu.sx.pojo.BW;
import com.liu.sx.service.DriverLoginService;
import com.liu.sx.service.DriverService;
import com.liu.sx.service.VoltageService;
import com.liu.sx.service.WormService;
import com.liu.sx.util.BCD2String;
import com.liu.sx.util.BwUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Component
@ChannelHandler.Sharable
@Slf4j
public class ServerHanlderUp extends SimpleChannelInboundHandler<BW> {




    @Autowired
   private WormService wormService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private VoltageService voltageService;

    @Autowired
    private DriverLoginService driverLoginService;




    @Override
    protected void messageReceived(ChannelHandlerContext ctx, BW bw) throws Exception {
//        设备地址和channel进行绑定
      try {
          System.out.println("channelId cun "+ ctx.channel().id());
          ChannelHolder.addClient(bw.getAddress(),ctx.channel());


          switch (bw.getData()[0]){
              case 0x20:
                  driverService.insert(bw);
                  driverLoginService.insert(bw,ctx.channel().remoteAddress().toString());
                  break;
              case 0x22:
//               todo : 电压，灯管，信号强度 待确定
//               日期 3个字节  |  96个点 96个字节 （15分钟一个） |  电压 1个字节 | 灯管状态 2 | 信号强度
//                 日期解析


                  wormService.insert(bw);
                  voltageService.insert(bw);
                  break;
          }



          byte[] time_bcd = BCD2String.time2BCD();
          bw.setLength(8);
          byte[] command = {0x04};
          byte[] data = ArrayUtils.addAll(command,time_bcd);
          bw.setData(data);
          ctx.writeAndFlush(bw);

          ReferenceCountUtil.release(bw);
      }catch (Exception e){
        log.error("异常",e);

      }


    }


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {


        super.channelInactive(ctx);

       ChannelHolder.removeClientId(ctx.channel());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
        log.error("异常信息",cause);
        System.out.println("netty 异常信息"+cause.getMessage());
        super.exceptionCaught(ctx, cause);
    }
}
